package io.bluerain.g4_tools.utils;

import java.io.DataOutputStream;
import java.io.File;
import java.io.OutputStream;

/**
 * Created by foredawn on 15-9-14.
 * shell工具类
 */
public class Shell {

    public static String SYSTEM = "/system";

    public static void su(String cmd) {
        cmd("su && " + cmd);
    }

    public static void su() {
        cmd("su");
    }

    public static void cmd(String cmd) {
        try {
            //权限设置
            Process p = Runtime.getRuntime().exec(cmd);
            System.out.println(p.waitFor());
            //获取输出流
            OutputStream outputStream = p.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            //将命令写入
            dataOutputStream.writeBytes(cmd);
            //提交命令
            dataOutputStream.flush();
            //关闭流操作
            dataOutputStream.close();
            outputStream.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        System.out.println(cmd);
    }

    public static String append(String cmd) {
        return cmd + " &&";
    }

    public static void afterMountExe(String mount) {
        su("mount -o remount rw " + mount);
    }
}
